//
//  HLTloginTableViewCell.m
//  huiliantong
//
//  Created by hlt on 15/8/28.
//  Copyright (c) 2015年 hlt. All rights reserved.
//

#import "HLTloginTableViewCell.h"
#import "HLTzhuceTableViewController.h"
#import "HLTWangjipasswordTableViewController.h"
@implementation HLTloginTableViewCell

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (IBAction)zhuce:(id)sender {
    //取消键盘
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    HLTzhuceTableViewController *vc=[[HLTzhuceTableViewController alloc]init];
    [self.parent.navigationController pushViewController:vc animated:NO];
    
    
    
}


- (IBAction)wangjipassword:(id)sender {
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    HLTWangjipasswordTableViewController *vc=[[HLTWangjipasswordTableViewController alloc]init];
    [self.parent.navigationController pushViewController:vc animated:NO];
    
    
    
}


- (IBAction)login:(id)sender {
    
    
    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
    
    UIView *view1=[[UIView alloc] initWithFrame:self.parent.view.bounds];
    view1.backgroundColor=[UIColor grayColor];
    view1.alpha=0.4;
    [self.parent.view addSubview:view1];
    MONActivityIndicatorView *indicatorView = [[MONActivityIndicatorView alloc] init];
    indicatorView.delegate = self;
    indicatorView.numberOfCircles = 3;
    indicatorView.radius = 20;
    indicatorView.internalSpacing = 3;
    indicatorView.center = self.parent.view.center;
    [indicatorView startAnimating];
    [self.parent.view addSubview:indicatorView];

    
    
    
    
    
    
    NSString *url=[loginurl stringByAppendingFormat:@"&username=%@&password=%@",self.tel.text,self.password.text];
    [HLTNetWorkingtool getyouhuilist:url success:^(NSMutableArray *ary) {
        
//        NSArray *aaary=ary;
        NSDictionary *dic=ary[0];
          NSLog(@"%@",dic[@"bind_card"]);
        if ([dic[@"status"] isEqual:@"0"]) {
            
            if ([dic[@"bind_card"] isEqual:@"00"]) {
                Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                acc.pwd=self.password.text;
                acc.custPhone=self.tel.text;
                acc.custFlag=dic[@"user_rank"];
                acc.cardNo=@"";
                [[AccountTool sharedAccountTool] addAccounts:acc];
                [self.parent.navigationController popViewControllerAnimated:YES];
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
                
                
            }
            else
            {
                
            
            
            NSString *url=[chaxunyue stringByAppendingFormat:@"&user_name=%@&cardNum=%@",self.tel.text,dic[@"bind_card"]];
            [HLTNetWorkingtool getdic:url success:^(NSDictionary *ary) {
                
                
                if ([ary[@"status"] isEqual:@"0"]) {
                    Account *acc=[[AccountTool sharedAccountTool] currentAccount];
                    acc.pwd=self.password.text;
                    acc.cardNo=dic[@"bind_card"];
                    acc.custFlag=dic[@"user_rank"];
                    acc.custPhone=self.tel.text;
                    acc.YKTCardBalance=ary[@"deposit"];
                    [[AccountTool sharedAccountTool] addAccounts:acc];
                    NSLog(@"%@",ary);
                    [self.parent.navigationController popViewControllerAnimated:YES];
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];

                }else
                {
                    [indicatorView stopAnimating];
                    [view1 removeFromSuperview];

                }
            } faliure:^(NSString *error) {
                [indicatorView stopAnimating];
                [view1 removeFromSuperview];
               
            }];
            }
            
            
            
        }else
        {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"登陆失败"   message:dic[@"msg"]  delegate:nil cancelButtonTitle:@"好" otherButtonTitles: nil];
            [alert show];
            [indicatorView stopAnimating];
            [view1 removeFromSuperview];

        }
        
        
        
        
    } faliure:^(NSString *error) {
        [indicatorView stopAnimating];
        [view1 removeFromSuperview];

    }];
    
    
    
    
    
    
}




#pragma mark - MONActivityIndicatorViewDelegate Methods

- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView
      circleBackgroundColorAtIndex:(NSUInteger)index {
    CGFloat red   = (arc4random() % 256)/255.0;
    CGFloat green = (arc4random() % 256)/255.0;
    CGFloat blue  = (arc4random() % 256)/255.0;
    CGFloat alpha = 1.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}



@end
